package com.gxa.ehome.dao.impl;

import com.gxa.ehome.dao.BaseDao;
import com.gxa.ehome.dao.UserDao;
import com.gxa.ehome.entity.User;
import com.gxa.ehome.util.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

/**
 * UserDaoImpl 作为UserDao的实现类
 *
 * @author bilei
 * @date 2021年3月8日
 */
public  class UserDaoImpl extends BaseDao<User> implements UserDao {

    // 引入DBUtil
    private DBUtil dbutil = new DBUtil();

    @Override
    public User selectByPhone(String phone) throws Exception {
        String sql = "SELECT * FROM tb_user WHERE user_phone=?";
        PreparedStatement preparedStatement = dbutil.getPreparedStatement(sql);
        preparedStatement.setObject(1, phone);
        ResultSet resultSet = dbutil.execQuery(preparedStatement);
        User user = null;
        if (resultSet.next()) {
            user = new User();
            user.setId(resultSet.getInt(1));// user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString(2));
            user.setPhone(resultSet.getString(3));
            user.setBrith(resultSet.getDate(4));
            user.setSex(resultSet.getString(5));
            user.setRegTime(resultSet.getDate(6));
        }
        dbutil.closeAll();
        return user;
    }
    @Override
    public Integer insert(User user) throws Exception {
        return super.insert(user);
    }

    @Override
    public Integer getCount() throws Exception {
        return super.getCount(User.class);
    }

    @Override
    public List<User> selectAlluser() throws Exception {
        return super.selectAll(User.class);
    }

    @Override
    public List<User> selectAllBypage(Integer page, Integer size) throws Exception {
        return super.selectByPage(page,size,User.class);
    }


}